package com.topvision.myviewtest.wallpager;

import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;

import java.io.IOException;

/**
 * @author lucy
 * @date 2019-01-28 15:56
 * @description //TODO
 */

public class LiveWallpaperService extends WallpaperService {
    @Override
    public Engine onCreateEngine() {
        return new WallPagerEngine();
    }


    /**
     * 设置壁纸
     *
     * @param context
     */
    public void set2WallPaper(Context context) {
        try {
            context.clearWallpaper();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
        intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(context, LiveWallpaperService.class));
        context.startActivity(intent);
    }

    class WallPagerEngine extends Engine implements Runnable {
        Thread mThread;
        SurfaceHolder mSurfaceHolder;
        private int mWidthPixels, mHeightPixels;
        private boolean loop = true;

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            mSurfaceHolder = holder;
            init();
            mThread = new Thread(this);
            mThread.start();
        }

        private void init() {
            mPaint = new Paint();
            mPaint.setColor(Color.RED);
            mWidthPixels = getResources().getDisplayMetrics().widthPixels;
            mHeightPixels = getResources().getDisplayMetrics().heightPixels;
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            System.out.println("visible:" + visible);
        }

        @Override
        public void run() {
            while (true) {
                draw();
            }
        }

        private void draw() {
            Canvas mCanvas = null;
            try {
                mCanvas = mSurfaceHolder.lockCanvas();
                mPaint.setShader(updateLinearGradient());
                mCanvas.drawRect(0, 0, mWidthPixels, mHeightPixels, mPaint);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (mCanvas != null) {
                    mSurfaceHolder.unlockCanvasAndPost(mCanvas);
                }
            }
        }

        Paint mPaint;
        private int[] colors = new int[2];
        public static final int START = 105;
        public static final int END = 255;
        public static final int DIFF = 30;
        /* 正向 */
        int color0;
        int color1;
        int color2;
        int color3;
        /* 反向 */
        int color4;
        int color5;
        int color6;
        int color7;
        /* 正向/方向 控制器 */
        boolean flag = true;

        public Shader updateLinearGradient() {
            LinearGradient linearGradient;
            if (flag) {
                color0++;
                if (color0 > DIFF) {
                    color1++;
                }
                if (START + color0 > END) {
                    color2++;
                }
                if (START + color1 > END) {
                    color3++;
                }
                // blue->red(105,105,255)->(255,105,105)
                colors[0] = Color.rgb((START + color1) > END ? END : (START + color1), START, (END - color3) < START ? START : (END - color3));
                colors[1] = Color.rgb((START + color0) > END ? END : (START + color0), START, (END - color2) < START ? START : (END - color2));
                linearGradient = new LinearGradient(0, 0, mWidthPixels, 30, colors, null, LinearGradient.TileMode.CLAMP);
                if (END - color3 < START) {
                    flag = false;
                    color0 = 0;
                    color1 = 0;
                    color2 = 0;
                    color3 = 0;
                }
            } else {
                color4++;
                if (color4 > DIFF) {
                    color5++;
                }
                if (START + color4 > END) {
                    color6++;
                }
                if (START + color5 > END) {
                    color7++;
                }
                // red->blue(255,105,105)->(105,105,255)
                colors[0] = Color.rgb(END - color7 < START ? START : (END - color7), START, (START + color5) > END ? END : (START + color5));
                colors[1] = Color.rgb(END - color6 < START ? START : (END - color6), START, (START + color4) > END ? END : (START + color4));
                linearGradient = new LinearGradient(0, 0, mWidthPixels, 30, colors, null, LinearGradient.TileMode.CLAMP);
                if (END - color7 < START) {
                    flag = true;
                    color4 = 0;
                    color5 = 0;
                    color6 = 0;
                    color7 = 0;
                }
            }
            return linearGradient;
        }
    }
}
